package com.thx.common.web;

import java.util.HashMap;
import java.util.Map;

/**
 * 操作结果.
 *
 * @author balance
 */
public class ActionResult {

  private String code;// 结果代码

  private String desc;// 结果描述

  private Map<String, Object> data;// 自定义数据

  public static final String SUCCESS = "success";
  public static final String FAIL = "fail";
  public static final String ERROR = "error";

  public static final String EXISTENT = "existent";
  public static final String NON_EXISTENT = "non-existent";

  public ActionResult(String code, String desc) {
    this.code = code;
    this.desc = desc;
  }

  public ActionResult(String code, String desc, Map<String, Object> data) {
    this.code = code;
    this.desc = desc;
    this.data = data;
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getDesc() {
    return desc;
  }

  public void setDesc(String desc) {
    this.desc = desc;
  }

  public Map<String, Object> getData() {
    return data;
  }

  public void setData(Map<String, Object> data) {
    this.data = data;
  }

  public void setVal(String key, Object val) {
    if (this.data == null) {
      this.data = new HashMap<String, Object>();
      this.data.put(key, val);
    } else {
      this.data.put(key, val);
    }
  }

  public String toJson() {
    String json = com.alibaba.fastjson.JSON.toJSONString(this);
    return json;
  }
}
